home *** CD-ROM | disk | FTP | other *** search
- unit Ndxrebu;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Buttons, DB, BDE, DBTables, ExtCtrls, ComCtrls;
-
- type
- TForm1 = class(TForm)
- cbAlias: TComboBox;
- cbTable: TComboBox;
- Label1: TLabel;
- Label2: TLabel;
- BitBtn1: TBitBtn;
- BitBtn2: TBitBtn;
- tblIndex: TTable;
- StatusBar1: TStatusBar;
- procedure FormCreate(Sender: TObject);
- procedure cbAliasChange(Sender: TObject);
- procedure BitBtn1Click(Sender: TObject);
- public
- function RebuildIndexes(strAlias, strTable: string;
- var strError: string): Boolean;
- procedure HandleExceptions(Sender: TObject; E: Exception);
- procedure WriteMsg(strWrite: string);
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure TForm1.HandleExceptions(Sender: TObject; E: Exception);
- begin
- If E.Message <> '' then
- begin
- Screen.Cursor := crArrow;
- MessageDlg(E.Message, mtError, [mbOK], 0);
- end;
- end;
-
- procedure TForm1.WriteMsg(strWrite: string);
- begin
- StatusBar1.Panels[0].Text := strWrite;
- StatusBar1.Update;
- end;
-
- function TForm1.RebuildIndexes(strAlias, strTable: string;
- var strError: string): Boolean;
- var
- bdeResult: DBIResult;
- begin
- Result := False;
- if tblIndex.Active then
- tblIndex.Active := False;
-
- tblIndex.DatabaseName := strAlias;
- tblIndex.TableName := strTable;
-
- Screen.Cursor := crHourglass;
- try
- WriteMsg('Opening ' + strTable + '...');
- tblIndex.Open;
- finally
- Screen.Cursor := crDefault;
- end;
-
- if not tblIndex.Active then
- strError := 'The table could not be opened exclusively. It is ' +
- 'probably being used by another user or application.'
- else begin
- WriteMsg('Regenerating indexes for ' + strTable + '...');
- Screen.Cursor := crHourglass;
- try
- bdeResult := DbiRegenIndexes(tblIndex.Handle);
- case bdeResult of
- DBIERR_NONE: Result := True;
- DBIERR_INVALIDHNDL: strError := 'Invalid table handle.';
- DBIERR_NEEDEXCLACCESS: strError := 'Table is open in shared mode.';
- DBIERR_NOTSUPPORTED: strError := 'Remote indexes cannot be rebuilt.';
- else
- strError := 'Unexpected Error Returned by BDE.';
- end;
- finally
- Screen.Cursor := crDefault;
- end;
- end;
- WriteMsg( '' );
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- { Set up the exception handler. }
- Application.OnException := HandleExceptions;
-
- { Populate the Alias drop down with the currently
- defined aliases. }
-
- Screen.Cursor := crHourglass;
- try
- Session.GetAliasNames(cbAlias.Items);
- cbAlias.ItemIndex := 0;
- finally
- Screen.Cursor := crDefault;
- end;
-
- { Now, get the table name for the first index in the list. }
- cbAliasChange(nil);
- end;
-
- procedure TForm1.cbAliasChange(Sender: TObject);
- begin
- { Get the tables in the new index. }
- Screen.Cursor := crHourglass;
- try
- with cbAlias do
- Session.GetTableNames(Items[ItemIndex],
- '', TRUE, FALSE, cbTable.Items );
- cbTable.Items.Insert(0, '<All Tables>');
- cbTable.ItemIndex := 0;
- finally
- Screen.Cursor := crDefault;
- end;
- end;
-
- procedure TForm1.BitBtn1Click(Sender: TObject);
- var
- strTable: string;
- Counter: Integer;
- iNTables: Integer;
- strError: string;
- begin
- strError := '';
- if cbTable.ItemIndex > 0 then
- begin
- if not RebuildIndexes(cbAlias.Items[cbAlias.ItemIndex],
- cbTable.Items[cbTable.ItemIndex], strError) then
- MessageDlg('Unable to rebuild indexes for ' +
- cbTable.Items[ cbTable.ItemIndex ] +
- '. Reason: ' + '. ' + #10 + #10 +
- strError, mtError, [mbOK], 0);
- end
- else begin
- iNTables := cbTable.Items.Count;
- for Counter := 1 to cbTable.Items.Count - 1 do
- begin
- StatusBar1.Panels[1].Text := IntToStr(iNTables - Counter);
- StatusBar1.Update;
- if not RebuildIndexes(cbAlias.Items[cbAlias.ItemIndex],
- cbTable.Items[Counter], strError) then
- MessageDlg('Unable to rebuild indexes for ' +
- cbTable.Items[Counter] + '. ' + #10 + #10 +
- 'Reason: ' + strError, mtError, [mbOK], 0 );
- end;
- StatusBar1.Panels[1].Text := '';
- StatusBar1.Update;
- end;
- end;
- end.